package kr.co.kiso.usr.am.gr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.kiso.usr.am.gr.service.AMGRService;
import kr.co.kiso.usr.am.gr.vo.AMGRVo;
import kr.co.kiso.usr.common.BaseController;
import kr.co.kiso.usr.utils.PaginationInfo;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


/**
 * 관리자 관리, 그룹관리, 메뉴관리
 * @author devhw
 *
 */
@Controller
@RequestMapping("/dsmgr/am/gr/*")
public class AMGRController extends BaseController{
	@Autowired
	private AMGRService amgrService;

	Logger log = Logger.getLogger(getClass());
	
	/**
	 * 관리자 조회
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr010Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
//		private String src_admin_nm;
//		private String src_admin_id;
//		private String src_reg_dt;
		if(vo.getSrc_admin_nm()!=null && !vo.getSrc_admin_nm().equals("")){
			vo.setSrc_admin_nm("%"+vo.getSrc_admin_nm()+"%");
		}
		if(vo.getSrc_admin_id()!=null && !vo.getSrc_admin_id().equals("")){
			vo.setSrc_admin_id("%"+vo.getSrc_admin_id()+"%");
		}
		if(vo.getSrc_reg_dt()!=null && !vo.getSrc_reg_dt().equals("")){
			
			vo.setSrc_reg_dt("%"+vo.getSrc_reg_dt().replaceAll("\\.","")+"%");
		}
		
		ModelAndView mav = new ModelAndView();
		
		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));

		int iTotalCnt = amgrService.amgr010Q_cnt(vo);
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			// 상위 코드목록
			mav.addObject("list", amgrService.amgr010Q(vo));
			
			mav.addObject("pageHtml", vo.getToHtml("./amgr010Q.do"));
			mav.addObject("paginationInfo", vo);
		}
//
		return mav;
	}
	
	/**
	 * 관리자관리 등록 폼
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr020Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("groupList",amgrService.amgr022Q(vo) );
		
		return mav;
	}
	
	/**
	 * 관리자 관리 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr020T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr010Q.do");
		vo.setAdmin_seq_no(amgrService.amgr021Q());
		

//		vo.setAdmin_pw(secu.Enc("ADMIN_USR","admin_pw",vo.getAdmin_pw()));
		
		amgrService.amgr020T(vo);
		return mav;
	}
	
	/**
	 * 수정폼
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr030Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("map",amgrService.amgr030Q(vo) );
		mav.addObject("groupList",amgrService.amgr022Q(vo) );
		
		return mav;
	}
	
	/**
	 * 
	 * 수정프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr030T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr010Q.do");
		

//		vo.setAdmin_pw(secu.Enc("ADMIN_USR","admin_pw",vo.getAdmin_pw()));
		
		
		mav.addObject("map",amgrService.amgr030T(vo) );
		
		return mav;
	}
	
	/**
	 * 삭제 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr040T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr010Q.do");
		
		mav.addObject("map",amgrService.amgr040T(vo) );
		
		return mav;
	}
	
	/**
	 * 그룹관리 조회
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr050Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
//		private String src_admin_nm;
//		private String src_admin_id;
//		private String src_reg_dt;
		if(vo.getSrc_admin_nm()!=null && !vo.getSrc_admin_nm().equals("")){
			vo.setSrc_admin_nm("%"+vo.getSrc_admin_nm()+"%");
		}
		if(vo.getSrc_admin_id()!=null && !vo.getSrc_admin_id().equals("")){
			vo.setSrc_admin_id("%"+vo.getSrc_admin_id()+"%");
		}
		if(vo.getSrc_reg_dt()!=null && !vo.getSrc_reg_dt().equals("")){
			
			vo.setSrc_reg_dt("%"+vo.getSrc_reg_dt().replaceAll("\\.","")+"%");
		}
		
		ModelAndView mav = new ModelAndView();
		
		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));

		int iTotalCnt = amgrService.amgr050Q_cnt(vo);
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			// 상위 코드목록
			mav.addObject("list", amgrService.amgr050Q(vo));
			List list = amgrService.amgr050Q(vo);
			List returnList = new ArrayList();
			for(int i =0; i<list.size();i++){
				Map map = (Map) list.get(i);
				
				
				returnList.add(map);
			}
			
			mav.addObject("pageHtml", vo.getToHtml("./amgr010Q.do"));
			mav.addObject("paginationInfo", vo);
		}
//
		return mav;
	}
	
	/**
	 * 그룹 등록
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr060Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView();
		
//		mav.addObject("adminList",amgrService.amgr060Q(vo) );
		mav.addObject("menuList",amgrService.amgr061Q(vo) );
		
		return mav;
	}
	
	/**
	 * 그룹 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr060T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr050Q.do");
		
		vo.setGroup_seq_no(amgrService.amgr062Q(vo) );
		amgrService.amgr060T(vo);
		
		for(int i=0; i< vo.getTopMenu1().length;i++){
			vo.setMenu_seq_no(Integer.parseInt(vo.getTopMenu1()[i]) );
			amgrService.amgr061T(vo);
		}
		for(int i=0; i< vo.getTopMenu2().length;i++){
			vo.setMenu_seq_no(Integer.parseInt(vo.getTopMenu2()[i]) );
			amgrService.amgr061T(vo);
		}
		for(int i=0; i< vo.getTopMenu3().length;i++){
			vo.setMenu_seq_no(Integer.parseInt(vo.getTopMenu3()[i]) );
			amgrService.amgr061T(vo);
		}
		
		return mav;
	}
	
	/**
	 * 그룹 수정 폼
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr070Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView();
		mav.addObject("map1",amgrService.amgr071Q(vo) );
		mav.addObject("adminList",amgrService.amgr060Q(vo) );
		mav.addObject("menuList",amgrService.amgr070Q(vo) );

		
		return mav;
	}
	/**
	 * 그룹 수정 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr070T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr050Q.do");
		
		amgrService.amgr071T(vo);
		amgrService.amgr070T(vo);
		
		for(int i=0; i< vo.getTopMenu1().length;i++){
			vo.setMenu_seq_no(Integer.parseInt(vo.getTopMenu1()[i]) );
			amgrService.amgr061T(vo);
		}
		for(int i=0; i< vo.getTopMenu2().length;i++){
			vo.setMenu_seq_no(Integer.parseInt(vo.getTopMenu2()[i]) );
			amgrService.amgr061T(vo);
		}
		for(int i=0; i< vo.getTopMenu3().length;i++){
			vo.setMenu_seq_no(Integer.parseInt(vo.getTopMenu3()[i]) );
			amgrService.amgr061T(vo);
		}
		
		return mav;
	}
	
	
	/**
	 * 메뉴관리 조회
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr100Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
//		private String src_admin_nm;
//		private String src_admin_id;
//		private String src_reg_dt;
		if(vo.getSrc_admin_nm()!=null && !vo.getSrc_admin_nm().equals("")){
			vo.setSrc_admin_nm("%"+vo.getSrc_admin_nm()+"%");
		}
		if(vo.getSrc_admin_id()!=null && !vo.getSrc_admin_id().equals("")){
			vo.setSrc_admin_id("%"+vo.getSrc_admin_id()+"%");
		}
		if(vo.getSrc_reg_dt()!=null && !vo.getSrc_reg_dt().equals("")){
			
			vo.setSrc_reg_dt("%"+vo.getSrc_reg_dt().replaceAll("\\.","")+"%");
		}
		
		ModelAndView mav = new ModelAndView();
		
		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));

		int iTotalCnt = amgrService.amgr100Q_cnt(vo);
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			// 상위 코드목록
			mav.addObject("list", amgrService.amgr100Q(vo));
			
			mav.addObject("pageHtml", vo.getToHtml("./amgr010Q.do"));
			mav.addObject("paginationInfo", vo);
		}
//
		return mav;
	}
	
	/**
	 * 메뉴 등록 폼
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr110Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		//isMember(req,res);
		ModelAndView mav = new ModelAndView();
		
		return mav;
	}
	
	/**
	 * 메뉴 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr110T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		String returnUrl = "";
		if(vo.getP_menu_seq_no()==0){
			returnUrl="redirect:./amgr100Q.do";
		}else{
			returnUrl="redirect:./amgr120Q.do?menu_seq_no="+vo.getP_menu_seq_no();
		}
//		"amgr120Q.do?menu_seq_no=1"
		ModelAndView mav = new ModelAndView(returnUrl);
		vo.setMenu_seq_no(amgrService.amgr111Q(vo));
		vo.setMenu_order(amgrService.amgr1112(vo));
		amgrService.amgr110T(vo);

		return mav;
	}
	
	/**
	 * 상세 조회
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr120Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView();
		mav.addObject("map", amgrService.amgr120Q(vo));
		mav.addObject("list", amgrService.amgr121Q(vo));
		
		return mav;
	}
	
	/**
	 * 메뉴 수정 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr120T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr120Q.do?menu_seq_no="+vo.getMenu_seq_no());
		amgrService.amgr120T(vo);
		
		return mav;
	}
	
	/**
	 * 메뉴 삭제 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr130T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr120Q.do?menu_seq_no="+vo.getMenu_seq_no());
		
		
		return mav;
	}
	
	
	/**
	 * 코드 목록
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr150Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
//		private String src_admin_nm;
//		private String src_admin_id;
//		private String src_reg_dt;
		if(vo.getSrc_admin_nm()!=null && !vo.getSrc_admin_nm().equals("")){
			vo.setSrc_admin_nm("%"+vo.getSrc_admin_nm()+"%");
		}
		if(vo.getSrc_admin_id()!=null && !vo.getSrc_admin_id().equals("")){
			vo.setSrc_admin_id("%"+vo.getSrc_admin_id()+"%");
		}
		if(vo.getSrc_reg_dt()!=null && !vo.getSrc_reg_dt().equals("")){
			
			vo.setSrc_reg_dt("%"+vo.getSrc_reg_dt().replaceAll("\\.","")+"%");
		}
		
		ModelAndView mav = new ModelAndView();
		
		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));

		int iTotalCnt = amgrService.amgr150Q_cnt(vo);
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			// 상위 코드목록
			mav.addObject("list", amgrService.amgr150Q(vo));
			
			mav.addObject("pageHtml", vo.getToHtml("./amgr010Q.do"));
			mav.addObject("paginationInfo", vo);
		}
//
		return mav;
	}
	
	/**
	 * 코드 등록폼
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr160Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);

		ModelAndView mav = new ModelAndView();
		
		return mav;
	}
	
	/**
	 * 코드 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr160T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr170Q.do?code_seq_no="+vo.getP_code_seq_no());
		vo.setCode_seq_no(amgrService.amgr160Q(vo));
		amgrService.amgr160T(vo);
		
		return mav;
	}
	
	/**
	 * 코드 상세 조회 폼
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr170Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView();
		mav.addObject("map", amgrService.amgr170Q(vo));
		mav.addObject("list", amgrService.amgr171Q(vo));
		
		return mav;
	}
	
	/**
	 * 코드 수정 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr170T(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./amgr170Q.do?code_seq_no="+vo.getCode_seq_no());
		amgrService.amgr170T(vo);
		
		return mav;
	}
	
	/**
	 * 하위코드 json으로 가져오기
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr200Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		//isMember(req,res);
		ModelAndView mav = new ModelAndView("/common/toJson");
		
		Map returnMap = new HashMap();
		
		returnMap.put("codeList", amgrService.amgr171Q(vo));
		
		mav.addObject("map",returnMap) ;
		
		return mav;
	}
}
